package com.jt;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication //启动注解
public class ProviderApp { //外部类
    //创建java中的日志对象(SLF4J是java中的日志规范,是日志对外的窗口,是门面)
    //目前市场上对[@SLF4J]规范的实现主要有两种:log4j,logback
    private static final Logger log=
            LoggerFactory.getLogger(ProviderController.class);

    public static void main(String[] args) {
        SpringApplication.run(ProviderApp.class);
    }

    @RefreshScope //动态刷新配置，当配置中心数据变化，给Value属性重新刷新
    @RestController
    public class ProviderController{ //内部类

        public  ProviderController(){ //无参构造
            System.out.println("===构造方法启动，创建对象==");
        }

        @Value("${server.port:8080}")
        private  String server;
        //http://localhost:8082/provider/get/{msg}
        @GetMapping("/provider/get/{msg}")
        public  String get(@PathVariable  String msg) throws InterruptedException {
            //Thread.sleep(5000);//服务方:模拟耗时操作,并抛出异常，用于测试
            return  server+"获取信息"+msg;//返回前端页面
        }

        //为【配置中心平台动态发布】不再重启tomcat服务器；需要在nacos发布yml即可
        @Value("${logging.level.com.jt:debug}")//error是给的默认值
       private String  LogLevel;
        // http://localhost:8082/provider/getLogLevel
        @GetMapping("/provider/getLogLevel")
        public  String getLogLevel(){
            //五大日志级别：trace<debug<info<warn<error
            log.trace("===trace===");
            log.debug("===debug===");
            log.info("===info===");
            log.warn("===warn===");
            log.error("===error===");
            return "log level is "+LogLevel;
        }

//添加属性和方法用于获取和输出DEV_GROUP_51配置中【设置的线程数】
        @Value("${server.tomcat.threads.max:200}")//默认配置200，配置中心是300
        private Integer maxThread;
// http://localhost:8082/provider/getMaxThread
        @RequestMapping("/provider/getMaxThread")
        public String doGetMaxThread(){
            return "server.threads.max is  "+maxThread;
        }

        //添加属性和方法用于【读取和应用共享配置】
        @Value("${page.pageSize:5}")//给的默认每页五条，配置中心为10条
        private Integer pageSize;
        // http://localhost:8082/provider/getPageSize
        @GetMapping("/provider/getPageSize")
        public String doGetPageSize(){
            //return String.format()
            return "page size is "+pageSize;
        }


    }


}

